﻿(function($){


    // $.ajax({
    //     url:"selectasSystemconfig",
    //     data:"",
    //     type:"get",
    //     dataType:"json",
    //     success:function (data){
    //         $('#dg4').datagrid({data:showList4(data)}).datagrid('clientPaging');
    //     },
    //     error:function (error) {
    //         alert("请求出错！")
    //     }
    // })

    // 分页显示和数据表格
    function pagerFilter(data){
            if ($.isArray(data)){    // is array
                data = {
                    total: data.length,
                    rows: data
                }
            }
            var target = this;
            var dg = $(target);
            var state = dg.data('datagrid');
            var opts = dg.datagrid('options');
            if (!state.allRows){
                state.allRows = (data.rows);
            }
            if (!opts.remoteSort && opts.sortName){
                var names = opts.sortName.split(',');
                var orders = opts.sortOrder.split(',');
                state.allRows.sort(function(r1,r2){
                    var r = 0;
                    for(var i=0; i<names.length; i++){
                        var sn = names[i];
                        var so = orders[i];
                        var col = $(target).datagrid('getColumnOption', sn);
                        var sortFunc = col.sorter || function(a,b){
                            return a==b ? 0 : (a>b?1:-1);
                        };
                        r = sortFunc(r1[sn], r2[sn]) * (so=='asc'?1:-1);
                        if (r != 0){
                            return r;
                        }
                    }
                    return r;
                });
            }
            var start = (opts.pageNumber-1)*parseInt(opts.pageSize);
            var end = start + parseInt(opts.pageSize);
            data.rows = state.allRows.slice(start, end);
            return data;
        }
        var loadDataMethod = $.fn.datagrid.methods.loadData;
        var deleteRowMethod = $.fn.datagrid.methods.deleteRow;
        $.extend($.fn.datagrid.methods, {
            clientPaging: function(jq){
                return jq.each(function(){
                    var dg = $(this);
                    var state = dg.data('datagrid');
                    var opts = state.options;
                    opts.loadFilter = pagerFilter;
                    var onBeforeLoad = opts.onBeforeLoad;
                    opts.onBeforeLoad = function(param){
                        state.allRows = null;
                        return onBeforeLoad.call(this, param);
                    }
                    var pager = dg.datagrid('getPager');
                    pager.pagination({
                        onSelectPage:function(pageNum, pageSize){
                            opts.pageNumber = pageNum;
                            opts.pageSize = pageSize;
                            pager.pagination('refresh',{
                                pageNumber:pageNum,
                                pageSize:pageSize
                            });
                            dg.datagrid('loadData',state.allRows);
                        }
                    });
                    $(this).datagrid('loadData', state.data);
                    if (opts.url){
                        $(this).datagrid('reload');
                    }
                });
            },
            loadData: function(jq, data){
                jq.each(function(){
                    $(this).data('datagrid').allRows = null;
                });
                return loadDataMethod.call($.fn.datagrid.methods, jq, data);
            },
            deleteRow: function(jq, index){
                return jq.each(function(){
                    var row = $(this).datagrid('getRows')[index];
                    deleteRowMethod.call($.fn.datagrid.methods, $(this), index);
                    var state = $(this).data('datagrid');
                    if (state.options.loadFilter == pagerFilter){
                        for(var i=0; i<state.allRows.length; i++){
                            if (state.allRows[i] == row){
                                state.allRows.splice(i,1);
                                break;
                            }
                        }
                        $(this).datagrid('loadData', state.allRows);
                    }
                });
            },
            getAllRows: function(jq){
                return jq.data('datagrid').allRows;
            }
        })

})(jQuery);


// 代理商账户余额报表
function showList1(data){
    var rows =[];
    for(var i=0;i<data.asA1.length;i++){
        var da = data.asA1[i];
        rows.push({
            inv: da.username,
            date: da.accountmoney
        });
    }
    return rows;
}

// 预付款流水报表
function showList2(data){
    var rows =[];
    for(var i=0;i<data.asA2.length;i++){
        var da = data.asA2[i];
        rows.push({
            inv:da.username,
            date:da.money,
            name:da.accountmoney,
            amount:da.memo,
            price:da.detaildatetime
        });
    }
    return rows;
}

// 代理商流水报表-
function showList3(data){
    var rows =[];
    for(var i=0;i<data.asA3.length;i++){
        var da = data.asA3[i];
        rows.push({
            inv:da.username,
            date:da.money,
            name:da.accountmoney,
            amount:da.memo,
            price:da.detaildatetime
        });
    }
    return rows;
}

// 点击查询
function inquire(){

    var detailDateTime1 = $("#s1").val();
    var detailDateTime2 = $("#s2").val();

    var msg = "";
    if(detailDateTime1!="" && detailDateTime1 != null){
        msg += "detailDateTime1="+detailDateTime1;
    }

    if(detailDateTime2!="" && detailDateTime2 != null){
        if(msg != "" && msg != null){
            msg += "&detailDateTime2="+detailDateTime2;
        }
        msg += "detailDateTime2="+detailDateTime2;
    }

    xianshi(msg);
}

function xianshi(msg) {
    $.ajax({
        url:"accountdetailInfo?"+msg,
        data:"",
        type:"get",
        dataType:"json",
        success:function (data){
            // 代理商账户余额报表
            if($("#u2_input").val() == "代理商账户余额报表"){
                $("#u3").siblings().hide()
                $("#u3").show();
                $('#dg1').datagrid({data:showList1(data)}).datagrid('clientPaging');
            }else if($("#u2_input").val() == "预付款流水报表" ){
                $("#u4").siblings().hide()
                $("#u4").show();
                $('#dg2').datagrid({data:showList2(data)}).datagrid('clientPaging');
            }else if($("#u2_input").val() == "代理商流水报表"){
                $("#u5").siblings().hide()
                $("#u5").show();
                $('#dg3').datagrid({data:showList3(data)}).datagrid('clientPaging');
            }
        },
        error:function (error) {
            alert("请求出错！")
        }
    })
}

// 产品分类数量/金额汇总
// function showList4(data){
//     var rows =[];
//     for(var i=0;i<data.length;i++){
//         var da = data[i];
//         rows.push({
//             //inv: da.preregfrozenmoney,
//             date: da.count
//             //name: da.price
//         });
//     }
//     return rows;
// }

// 客户消费汇总
// function showList5(data){
//     var rows =[];
//     for(var i=0;i<data.length;i++){
//         var da = data[i];
//         rows.push({
//             inv: da.username,
//             date: da.accountmoney
//         });
//     }
//     return rows;
// }




